package project2;

import gui.database;
import gui.real_project;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class login_2 extends JFrame {
    private JPanel jPanel1 = new JPanel();
    private JLabel jLabel1 = new JLabel();
    private JLabel jLabel2 = new JLabel();
    private JButton jButton1 = new JButton();
    private JButton jButton2 = new JButton();
    private JButton jButton3 = new JButton();
    private JTextField jTextField1 = new JTextField();
    private JPasswordField jPasswordField1 = new JPasswordField();
    private ButtonGroup b=new ButtonGroup();

    public login_2() {
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


   
    private void jbInit() throws Exception {
        this.getContentPane().setLayout( null );
        this.setSize(new Dimension(419, 291));
        this.setBounds(430,200, 430, 318);
        jPanel1.setBounds(new Rectangle(0, 0, 415, 265));
        jPanel1.setLayout(null);
        jLabel1.setText("Username :");
        jLabel1.setBounds(new Rectangle(30, 45, 100, 30));
        jLabel2.setText("Password :");
        jLabel2.setBounds(new Rectangle(30, 90, 95, 30));
        jButton1.setText("Login !");
        jButton1.setBounds(new Rectangle(30, 185, 75, 25));
        jButton1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e)  {
                    try {
                        jButton1_actionPerformed(e);
                    } catch (SQLException f) {
                    }
                    
                }
            });
        jButton2.setText("Reset");
        jButton2.setBounds(new Rectangle(105, 185, 80, 25));
        jButton2.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    jTextField1.setText("");
                    jPasswordField1.setText("");
                    
                }
            });
        jButton3.setText("Back");
        jButton3.setBounds(new Rectangle(185, 185, 80, 25));
        jButton3.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                login_2.this.setVisible(false);
                }
            });
        jPasswordField1.setBounds(new Rectangle(140, 95, 145, 25));
        jTextField1.setBounds(new Rectangle(140, 50, 145, 25));
        jPanel1.add(jTextField1, null);
        jPanel1.add(jPasswordField1, null);
        jPanel1.add(jButton3, null);
        jPanel1.add(jButton2, null);
        jPanel1.add(jButton1, null);
        jPanel1.add(jLabel2, null);
        jPanel1.add(jLabel1, null);
        this.getContentPane().add(jPanel1, null);
        this.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }


    private void jButton1_actionPerformed(ActionEvent e) throws SQLException {
        String c=new String(jPasswordField1.getPassword());
                             if (jTextField1.getText().isEmpty() &&  c.isEmpty()) {
                                 JOptionPane.showMessageDialog(null, "You Forgot Username And Password ");
                             }else if(jTextField1.getText().isEmpty()){
                                 JOptionPane.showMessageDialog(null, "You Forget Username");
                             }else if(c.isEmpty()){
                                 JOptionPane.showMessageDialog(null, "You Forget Password");
                             }
                            
                             else if(!jTextField1.getText().isEmpty()&&!c.isEmpty() )
                                 {
                                 String select="select * from users where name='"+jTextField1.getText()+"' and password='"+c+"' ";
                                     database d=new database();
                                     
                                     ResultSet rs=d.check(select);
                                     if(rs.next()){
                                         real_project l=new real_project();    
                                         this.setVisible(false);
                                     }else{
                                         JOptionPane.showMessageDialog(null, " error login please , try again "+jPasswordField1.getPassword().toString());
                                     }
                                 }     
                                     
                                 
    }
}
